<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Repository\Attraction_user;

use Plugin\Attractions\AttractionManagement\Model\Attraction_user\AttractionUser;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 用户表 Repository类
 */
class AttractionUserRepository extends IRepository
{
   public function __construct(
        protected readonly AttractionUser $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 用户ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 用户名
        if (isset($params['username']) && !empty($params['username'])) {
            $query->where('username', 'like', '%'.$params['username'].'%');
        }

        // 用户头像
        if (isset($params['avatar']) && !empty($params['avatar'])) {
            $query->where('avatar', '=', $params['avatar']);
        }

        // 用户登录次数
        if (isset($params['times']) && !empty($params['times'])) {
            $query->where('times', '=', $params['times']);
        }

        // 微信OpenID
        if (isset($params['open_id']) && !empty($params['open_id'])) {
            $query->where('open_id', 'like', '%'.$params['open_id'].'%');
        }

        // 用户状态: 1正常, 2禁用
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}